package com.esoon.cdms.utils;

import java.awt.Desktop;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.swing.JOptionPane;

public class FileUtil {
	public static void main(String[] args) {

		// String zipFilePath =
		// "F:/自己/FTPLocal/TA600/zhangsan_1459835390334.zip";
//		String zipFilePath = "F:/自己/FTPLocal/TA600";
//		File zipFile = new File(zipFilePath);
//		// zipFile.delete();
//		if (zipFile.exists()) {
//			try {
//				Desktop.getDesktop().open(zipFile);
//			} catch (IOException e1) {
//				e1.printStackTrace();
//			}
//		} else {
//			JOptionPane.showMessageDialog(null, "找不到指定的文件或目录！");
//		}
		System.out.println(getStringFromFile("E:\\MentorGraphics\\Capital2014.1\\plugins\\AllAvailableCheckOutDesign.xml"));

	}

	public void openDir(String dirPath) {
		String zipFilePath = dirPath;
		File zipFile = new File(zipFilePath);
		if (zipFile.exists()) {
			try {
				Desktop.getDesktop().open(zipFile);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null, "找不到指定的文件或目录！");
		}
	}

	public void openFile(String filePath) {
		String zipFilePath = filePath;
		File zipFile = new File(zipFilePath);
		if (zipFile.exists()) {
			try {
				Desktop.getDesktop().open(zipFile);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null, "找不到指定的文件或目录！");
		}
	}


	/**
	 * 从文件中获取字符串
	 * @param FileName
	 * @return
	 * @throws Exception
	 */
	public static String getStringFromFile(String FileName){
		byte[] data = null;
		try {
			BufferedInputStream bufferedInputStream = new BufferedInputStream(
					new FileInputStream(FileName));
			ByteArrayOutputStream memStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[2048];
			int len = 0;
			while ((len = bufferedInputStream.read(buffer)) != -1) {
				memStream.write(buffer, 0, len);
			}
			data = memStream.toByteArray();
			bufferedInputStream.close();
			memStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
		}
		return new String(data);
	}
	/**
	 * 从文件中获取字符串
	 * @param FileName
	 * @return
	 * @throws Exception
	 */
	public static String getStringFromInputStream(InputStream is){
		byte[] data = null;
		BufferedInputStream bufferedInputStream = null;
		ByteArrayOutputStream memStream = null;
		try {
			 bufferedInputStream = new BufferedInputStream(is);
			 memStream = new ByteArrayOutputStream();
			byte[] buffer = new byte[20480];
			int len = 0;
			while ((len = bufferedInputStream.read(buffer)) != -1) {
				memStream.write(buffer, 0, len);
			}
			data = memStream.toByteArray();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				bufferedInputStream.close();
				memStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		return new String(data);
	}
	
	/**
	 * 判断文件是否存在，如果不存在，则返回文件不存在，如果文件存在，但是大小为空，则返回文件大小为0kB.否则返回空
	 * @return
	 */
	public static String fileIsEmpty(String path){
		String result ;
		File file = new File(path);
		if(!file.exists()){
			result= "文件不存在!："+path;
		}else if(file.length()==0){
			result="文件大小为0KB!:"+path;
		}else{
			result = "文件正常！";
		}
		
		return result;
		
	}
}
